/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cong.javadanang.model.dao.impl;

import cong.javadanang.model.dao.CategoryDAO;
import cong.javadanang.model.entity.Category;
import cong.javadanang.model.entity.Post;
import cong.javadanang.model.entity.Question;
import hibernate.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Pham
 */
public class CategoryDAOImpl implements CategoryDAO {

    private static CategoryDAOImpl categoryDAO;

    public static CategoryDAOImpl getInstance() {
        if (categoryDAO == null) {
            categoryDAO = new CategoryDAOImpl();
        }
        return categoryDAO;
    }

    private Session session;
    private Transaction ts;

    @Override
    public Category getCategoryById(int catId) {
        session = HibernateUtil.getSessionFactory().openSession();
        Category category = null;
        try {
            ts = session.beginTransaction();
            category = (Category) session.get(Category.class, catId);
            List<Post> posts = session.createQuery("FROM Post as p Where p.category = :category and active = true")
                    .setParameter("category", category).list();
            category.setPosts(posts);
            List<Question> questions = session.createQuery("FROM Question as q Where q.category = :category and active = true")
                    .setParameter("category", category).list();
            category.setQuestions(questions);
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return category;
    }

    @Override
    public List<Category> getAllCategorys() {
        session = HibernateUtil.getSessionFactory().openSession();
        List<Category> categorys = null;
        try {
            ts = session.beginTransaction();
            categorys = session.createQuery("From Category ORDER BY categoryName").list();
            for (Category category : categorys) {
                List<Post> posts = session.createQuery("FROM Post as p Where p.category = :category and active = true")
                        .setParameter("category", category).list();
                category.setPosts(posts);
                List<Question> questions = session.createQuery("FROM Question as q Where q.category = :category and active = true")
                        .setParameter("category", category).list();
                category.setQuestions(questions);
            }
            ts.commit();
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
        }finally{
            session.close();
        }
        return categorys;
    }

    @Override
    public boolean createCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.save(cat);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean updateCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.update(cat);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean deleteCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            session.delete(cat);
            ts.commit();
            return true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean removeCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            String sql = "UPDATE Category set active = false WHERE categoryId = :categoryId";
            Query query = session.createQuery(sql);
            query.setParameter("categoryId", cat.getCategoryId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

    @Override
    public boolean restoreCategory(Category cat) {
        session = HibernateUtil.getSessionFactory().openSession();
        try {
            ts = session.beginTransaction();
            String sql = "UPDATE Category set active = true WHERE categoryId = :categoryId";
            Query query = session.createQuery(sql);
            query.setParameter("categoryId", cat.getCategoryId());
            query.executeUpdate();
            ts.commit();
            return true;
        } catch (HibernateException e) {
            if (ts != null) {
                ts.rollback();
            }
            e.printStackTrace();
            return false;
        }finally{
            session.close();
        }
    }

}
